package com.hunt.app.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hunt.model.app.PopularityStock;
import com.hunt.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ww
 * @Date : 2020/9/22 16:15
 */

@Api(value = "手机app端,查询最受欢迎股票集合")
@Controller
@RequestMapping("app/getPopularityStock")
public class PopularityStockController {

    /*@Autowired
    private RedisTemplate<String,Object> redisTemplate;*/

    @Autowired
    private JedisPool jedisPool;


    @ApiOperation(value = "分页查询热点股票", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getPopularityStockByPage", method = RequestMethod.GET)
    public Result getLecturerVideoByPositionSize(@RequestParam(value = "position",defaultValue = "0") Integer position,
                                                 @RequestParam(value = "size",defaultValue = "15") Integer size){
        /*List<Object> list = redisTemplate.boundListOps("POPULARITY_STOCK_LIST").range(position, position + size -1);
        if (list.size() == 0)
            return Result.success(list);*/

        List<Object> list = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            List<String> stringList = jedis.lrange("POPULARITY_STOCK_LIST", position, position + size -1);
            if (stringList.size() == 0) {
                return Result.success(stringList);
            }
            list = new ArrayList<>();
            for (String s : stringList) {
                list.add(JSONObject.parseObject(s, PopularityStock.class));
            }
            for (Object o : list) {
                PopularityStock popularityStock = (PopularityStock) o;
                //添加自己服务的涨跌幅
                List<String> stockData = jedis.hmget("Real_time_Stock_Data", popularityStock.getCode());
                if (null != stockData.get(0)){
                    JSONObject jsonObject = JSON.parseObject(stockData.get(0));
                    String hlPercent = jsonObject.getString("hlPercent");
                    popularityStock.setHlPercent(hlPercent);
                }
                //添加所属申万行业
                List<String> stock_base_message = jedis.hmget("Stock_Base_Message", popularityStock.getCode());
                if (null != stock_base_message.get(0)){
                    JSONObject jsonObject = JSONObject.parseObject(stock_base_message.get(0));
                    String industry = jsonObject.getString("industry");
                    popularityStock.setIndustry(industry);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return Result.success(list);
    }
}
